/**
 * jims
 */
package com.jims.master.modules.drug.service;

import com.jims.master.common.persistence.Page;
import com.jims.master.common.service.CrudService;
import com.jims.master.modules.drug.dao.DrugExportDetailDao;
import com.jims.master.modules.drug.dao.DrugExportMasterDao;
import com.jims.master.modules.drug.dao.DrugProvideApplicationDao;
import com.jims.master.modules.drug.dao.DrugStockDao;
import com.jims.master.modules.drug.entity.DrugExportDetail;
import com.jims.master.modules.drug.entity.DrugExportMaster;
import com.jims.master.modules.drug.entity.DrugProvideApplication;
import com.jims.master.modules.drug.entity.DrugStock;
import com.jims.master.modules.sys.entity.Dict;
import com.jims.master.modules.sys.utils.UserUtils;
import com.jims.master.modules.utils.DrugUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 药品发放生申请Service
 * @author zhuq
 * @version 2017-03-18
 */
@Service
@Transactional(readOnly = true)
public class DrugProvideApplicationService extends CrudService<DrugProvideApplicationDao, DrugProvideApplication> {
    @Autowired
    private DrugExportService drugExportService;
    @Autowired
    private DrugStockDao drugStockDao;
    @Autowired
    private DrugExportDetailDao drugExportDetailDao;
    @Autowired
    private DrugExportMasterDao drugExportMasterDao;

	public DrugProvideApplication get(String id) {
		return super.get(id);
	}
	
	public List<DrugProvideApplication> findList(DrugProvideApplication drugProvideApplication) {
		return super.findList(drugProvideApplication);
	}
	
	public Page<DrugProvideApplication> findPage(Page<DrugProvideApplication> page, DrugProvideApplication drugProvideApplication) {
		return super.findPage(page, drugProvideApplication);
	}
	
	@Transactional(readOnly = false)
	public int save(DrugProvideApplication drugProvideApplication) {
        int num = 0;
        if(null != drugProvideApplication && drugProvideApplication.getDrugProvideApplicationList().size() > 0){
            for (DrugProvideApplication provideApplication : drugProvideApplication.getDrugProvideApplicationList()) {
                provideApplication.setStorageIn(UserUtils.getUser().getOrgDept().getId());
                num += super.save(provideApplication);
            }
        }
		return num;
	}
	
	@Transactional(readOnly = false)
	public int delete(DrugProvideApplication drugProvideApplication) {
		return super.delete(drugProvideApplication);
	}

    /**
     * 查询申请出库的数据
     * @param drugProvideApplication
     * @return
     * @author fengyg
     */
    public List<DrugProvideApplication> findApplicationExportList(DrugProvideApplication drugProvideApplication) {
        return dao.findApplicationExportList(drugProvideApplication);
    }

    /**
     * 申请出库数据保存
     * @param drugProvideApplication
     * @return
     * @author fengyg
     */
    @Transactional(readOnly = false)
    public int saveProvideApplication(DrugProvideApplication drugProvideApplication){
        int num = 0;
        if(null != drugProvideApplication){
            DrugExportMaster drugExportMaster = new DrugExportMaster();  //出库主表
            drugExportMaster.preInsert();
            drugExportMaster.setStorageId(drugProvideApplication.getDrugProvideApplicationList().get(0).getStorageOut());
            drugExportMaster.setDocumentNo(drugExportService.createDocumentNo(drugExportMaster));
            drugExportMaster.setExportDate(new Date());
            drugExportMaster.setReceiver(drugProvideApplication.getDrugProvideApplicationList().get(0).getStorageIn());
            List<Dict> list = DrugUtils.getExportClassDictList();
            if(null != list && list.size() > 0){
                for (Dict dict : list) {
                    if(dict.getLabel().equals("发放")){
                        drugExportMaster.setExportClass(dict.getValue());
                    }
                }
            }
            drugExportMaster.updateOrgId();
            Double accountReceivable = 0.00;  //应收款

            //出库明细表
            List<DrugProvideApplication> applicationList = drugProvideApplication.getDrugProvideApplicationList();
            if(null != applicationList && applicationList.size() > 0){
                for(int i = 0; i < applicationList.size(); i++){
                    DrugStock stock = new DrugStock();
                    stock.updateOrgId();
                    stock.setInfoId(applicationList.get(i).getInfoId());
                    stock.setQuantity(applicationList.get(i).getQuantity() * applicationList.get(i).getPackageNum());
                    DrugStock stockQuantity = drugStockDao.findByInfoIdQuantity(stock);  //查询要减库存的库存信息
                    //减库存
                    stockQuantity.setQuantity((applicationList.get(i).getQuantity() - applicationList.get(i).getQuantityOut()) * applicationList.get(i).getPackageNum());
                    int update = drugStockDao.update(stockQuantity);
                    if(update > 0){
                        num += update;
                    }else{
                        return 0;
                    }
                    //拼接出库明细
                    DrugExportDetail detail = new DrugExportDetail();
                    detail.preInsert();
                    detail.setMasterId(drugExportMaster.getId());
                    detail.setItemNo((i + 1) + "");
                    detail.setInfoId(applicationList.get(i).getInfoId());
                    detail.setDrugCode(applicationList.get(i).getDrugCode());
                    detail.setPackageNum(applicationList.get(i).getPackageNum() + "");
                    detail.setPackageSpec(applicationList.get(i).getPackageSpec());
                    detail.setPackageUnits(applicationList.get(i).getPackageUnits());
                    detail.setMinSpec(applicationList.get(i).getMinSpec());
                    detail.setMinUnits(applicationList.get(i).getMinUnits());
                    detail.setBatchNo(stockQuantity.getBatchNo());  //批号
                    detail.setExpireDate(stockQuantity.getExpireDate());
                    detail.setPurchasePrice(stockQuantity.getPurchasePrice());
                    detail.setQuantity(applicationList.get(i).getQuantityOut());
                    detail.setInventory(applicationList.get(i).getQuantity() - applicationList.get(i).getQuantityOut());
                    detail.setDiscount("1");
                    detail.updateOrgId();
                    accountReceivable += applicationList.get(i).getQuantityOut() * applicationList.get(i).getPackageNum();  //计算应收款
                    int insert = drugExportDetailDao.insert(detail);  //出库明细保存
                    if(insert > 0){
                        num += insert;
                    }else{
                        return 0;
                    }
                    //修改申请记录的已发放库存数
                    DrugProvideApplication drugProvideApplication1 = dao.get(applicationList.get(i).getId());
                    Double newQuantityOut = drugProvideApplication1.getQuantityOut() + applicationList.get(i).getQuantityOut();
                    int updates = dao.updateQuantityOut(newQuantityOut, applicationList.get(i).getId());
                    if(updates > 0){
                        num += updates;
                    }else{
                        return 0;
                    }
                }
            }
            drugExportMaster.setAccountReceivable(accountReceivable);
            int insert = drugExportMasterDao.insert(drugExportMaster);  //出库主记录保存
            if(insert > 0){
                num += insert;
            }else{
                return 0;
            }
        }
        return num;
    }
	
}